package com.tarena.lbs.business.dao.repository.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class BusinessRepositoryImpl implements BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public List<BusinessPO> findPage(BusinessQuery query) {
        LambdaQueryWrapper<BusinessPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getBusinessName()),BusinessPO::getBusinessName, query.getBusinessName());
        wrapper.like(StringUtils.hasText(query.getBusinessHeadPhone()),BusinessPO::getBusinessHeadPhone, query.getBusinessHeadPhone());
        //修改前端逻辑
        if(query.getBusinessStatus() != null && query.getBusinessStatus() == 1){
            query.setBusinessStatus(2);
            wrapper.eq(BusinessPO::getBusinessStatus, query.getBusinessStatus());
        }else if(query.getBusinessStatus() != null && query.getBusinessStatus() == 0){
            query.setBusinessStatus(2);
            wrapper.ne(BusinessPO::getBusinessStatus, query.getBusinessStatus());
        }
        wrapper.between(query.getStartingTime() != null && query.getEndTime() != null,BusinessPO::getEntryTime, query.getStartingTime(), query.getEndTime());
        wrapper.orderByDesc(BusinessPO::getEntryTime);
        return businessMapper.selectList(wrapper);
    }


    @Override
    public BusinessPO selectByNickname(String businessName) {
        QueryWrapper<BusinessPO> wrapper = new QueryWrapper<>();
        wrapper.eq("business_name", businessName);
        return businessMapper.selectOne(wrapper);
    }

    @Override
    public Integer saveBusiness(BusinessParam param) {
        BusinessPO businessPO = new BusinessPO();
        BeanUtils.copyProperties(param,businessPO);
        businessMapper.insert(businessPO);
        return businessPO.getId();
    }

    @Override
    public BusinessPO selectById(Integer businessId) {
        return businessMapper.selectById(businessId);
    }

    @Override
    public void updateBusiness(BusinessPO businessPO) {
        businessMapper.updateById(businessPO);
    }
}
